package com.hgxd.controller;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.hgxd.annotation.OperationLogAnnotation;
import com.hgxd.entity.vo.pagevo.AlarmMsgPageVo;
import com.hgxd.service.IAlarmMsgService;
import io.swagger.annotations.Api;
import org.springframework.web.bind.annotation.*;
import io.swagger.annotations.ApiOperation;

import javax.annotation.Resource;
import java.util.List;
import java.util.Map;
import java.util.Objects;

import com.hgxd.result.Result;

import com.hgxd.entity.pojo.AlarmMsg;

import org.springframework.web.bind.annotation.RestController;

/**
 * <p>
 * 报警列表 前端控制器
 * </p>
 *
 * @author fsd
 * @since 2023-10-18
 */
@Api(tags = "报警列表")
@RestController
@RequestMapping("/api/alarmMsg")
@CrossOrigin
public class AlarmMsgController {
    @Resource(name = "AlarmMsgServiceImpl")
    private IAlarmMsgService alarmMsgService;

    /**
     * 根据id查询一个
     */
    @GetMapping("getById/{id}")
    @ApiOperation(value = "根据id查询一个", notes = "传入id")
    @OperationLogAnnotation(operModule = "报警列表-根据id查询一个", operType = "查询", operContent = "根据id查询一个")
    public Result<AlarmMsg> findOne(@PathVariable Integer id) {
        return Result.ok(alarmMsgService.getById(id));
    }

    /**
     * 分页模糊查询
     *
     * @param alarmMsgPageVo
     * @return
     */
    @PostMapping("/get/page")
    @ApiOperation(value = "分页模糊查询", notes = "传入对应的参数，组织传organizationId")
    @OperationLogAnnotation(operModule = "报警列表-分页模糊", operType = "查询", operContent = "分页模糊查询")
    public Result<Page<AlarmMsg>> findPage(@RequestBody AlarmMsgPageVo alarmMsgPageVo) {
        LambdaQueryWrapper<AlarmMsg> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(alarmMsgPageVo.getOrganizationId() != null
                        , AlarmMsg::getOrganizationId
                        , alarmMsgPageVo.getOrganizationId())
                .like(!"".equals(alarmMsgPageVo.getEquipmentName())
                        , AlarmMsg::getEquipmentName
                        , alarmMsgPageVo.getEquipmentName())
                .between(alarmMsgPageVo.getBeginTime() != null
                                && alarmMsgPageVo.getEndingTime() != null
                        , AlarmMsg::getAlarmTime
                        , alarmMsgPageVo.getBeginTime()
                        , alarmMsgPageVo.getEndingTime());
        // 倒序查询
        queryWrapper.orderByDesc(AlarmMsg::getId);
        return Result.ok(alarmMsgService.page(new Page<>(alarmMsgPageVo.getPageNum()
                , alarmMsgPageVo.getPageSize()), queryWrapper));
    }

    /**
     * 根据id派发任务
     *
     * @param alarmMsg
     * @return
     */
    @PostMapping("publishById")
    @ApiOperation(value = "根据id派发任务", notes = "必须传入id,其他无关参数传入空字符串，传参为id，principal，principalId，solver，solverId，taskContent")
    @OperationLogAnnotation(operModule = "报警列表-根据id派发任务", operType = "修改", operContent = "根据id派发任务")
    public Result<Boolean> publishById(@RequestBody AlarmMsg alarmMsg) {
        return Result.ok(alarmMsgService.publishById(alarmMsg));
    }

    /**
     * 根据id删除
     *
     * @param id
     * @return
     */
    @ApiOperation(value = "根据id删除", notes = "必须传入id")
    @OperationLogAnnotation(operModule = "报警列表-根据id删除", operType = "删除", operContent = "根据id删除")
    @GetMapping("/deleteById/{id}")
    public Result<Boolean> deleteById(@PathVariable("id") Integer id) {
        return Result.ok(alarmMsgService.removeById(id));
    }

    /**
     * 获取未读且未解决的报警列表
     *
     * @return
     */
    @ApiOperation(value = "获取未读且未解决的报警列表")
    @OperationLogAnnotation(operModule = "报警列表-获取未读且未解决的报警列表", operType = "查询", operContent = "获取未读且未解决的报警列表")
    @GetMapping("/getNoReadAndNoSolve")
    public Result<List<AlarmMsg>> getNoReadAndNoSolve() {
        LambdaQueryWrapper<AlarmMsg> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(AlarmMsg::getAlarmStatus, "未解决")
                .eq(AlarmMsg::getIsRead, 1);
        List<AlarmMsg> list = alarmMsgService.list(queryWrapper);
        return !list.isEmpty() ? Result.ok(list) : Result.fail();
    }

    @ApiOperation(value = "获取当天的报警信息")
    @OperationLogAnnotation(operModule = "报警列表-获取当天的报警信息", operType = "查询", operContent = "获取当天的报警信息")
    @GetMapping("/getAlarmMsgToday")
    public Result<Map<String, Object>> getAlarmMsgToday() {
        Map<String, Object> map = alarmMsgService.getAlarmMsgTody();
        return Result.ok(map);
    }

}

